import axios from 'axios'
import localStorage from 'store'

axios.defaults.baseURL = ''

export default function fetchProxy ({url, params, config = {}}) {

  let defaultConfig = {
    method: 'post',
    url: formateUrl(url),
    headers: {
      'Content-Type': 'application/json',
      'X-AUTH-TOKEN': localStorage.get('token')
    },
    data: params,
    params,
    ...config
  }

  if (defaultConfig.method === 'get') {
    delete defaultConfig.data
  }else {
    delete defaultConfig.params
  }

  return axios(defaultConfig)
}


function formateUrl (url) {
  if (url.match(/^http:\/\//)) return url
  let host = (location.host.match(/(stage|dev)(?:-)/) || [])[0]
  return `http://${host}mapi.yimifudao.com`
}